package com.chenfan.mcn.config;

import com.chenfan.common.exception.BusinessException;
import com.chenfan.mcn.exception.McnErrorCode;
import com.chenfan.mcn.extension.CodeStyleScanner;
import com.chenfan.mcn.extension.Res;
import com.chenfan.common.extension.lock.DistributedLockTemplate;
import com.chenfan.common.extension.mybatisplus.enhanced.EnhancedBaseMapper;
import com.chenfan.common.extension.context.SpringContextHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.StringRedisTemplate;

/**
 * @author wulg
 * @date 2023/12/7
 */

@Configuration
public class McnConfig {

    /**
     * 配置SpringContextHolder
     * @return
     */
    @Bean
    public SpringContextHolder getSpringContext() {
        return new SpringContextHolder();
    }


    /**
     * 配置代码规范扫描
     * @return
     */
    @Bean
    @ConditionalOnMissingBean
    public CodeStyleScanner codeStyleScanner() {
        return new CodeStyleScanner("com.chenfan", Res.class, EnhancedBaseMapper.class);
    }

    /**
     * 配置分布式锁
     * @param redisTemplate
     * @return
     */
    @Bean
    public DistributedLockTemplate distributedLockTemplate(@Autowired  StringRedisTemplate redisTemplate) {
        return new DistributedLockTemplate(redisTemplate, new BusinessException(McnErrorCode.LOCK_TRIGGER));
    }
}
